#include "nodeconfigureview.h"
#include "ui_nodeconfigureview.h"
#include <QWheelEvent>
#include "dboperate.h"

ComboBox::ComboBox(QWidget *parent) :
    QComboBox(parent)
{

}

ComboBox::~ComboBox()
{

}

void ComboBox::wheelEvent(QWheelEvent *event)
{

}

NodeConfigureView::NodeConfigureView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NodeConfigureView)
{
    ui->setupUi(this);
    setWindowTitle("监控站点配置");
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color: rgb(200, 255, 255);border: none;height:28px;}");
    ui->tableWidget->verticalHeader()->setHidden(true);
    DBOperate db;
    QVector<AreaInfo> areas= db.selectAreas();
    for (int i = 0; i < areas.length(); i ++)
    {
        this->areas.append(areas.at(i).areaName);
    }
}

NodeConfigureView::~NodeConfigureView()
{
    delete ui;
}

void NodeConfigureView::setTable(QVector<NodeInfo> nodesInfo)
{
    ui->tableWidget->setRowCount(nodesInfo.length());
    for (int i = 0; i < nodesInfo.length(); i++)
    {
        QString nodeName = nodesInfo.at(i).nodeName;
        QString nodePoint = nodesInfo.at(i).point;
        QString nodeID = nodesInfo.at(i).ID;
        //QString affiliatedArea = nodesInfo.at(0).affiliatedArea;
        QTableWidgetItem *item = new QTableWidgetItem;
        item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        ui->tableWidget->setItem(i, 1, item);
        ComboBox *combobox = new ComboBox;
        combobox->addItems(this->areas);
        combobox->setCurrentText(nodesInfo.at(i).affiliatedArea);
        comboboxs.append(combobox);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(nodeName));
        ui->tableWidget->setCellWidget(i, 1, combobox);
        //ui->tableWidget->setItem(i, 1, new QTableWidgetItem(affiliatedArea));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(nodeID));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(nodePoint));
    }
    ui->nodeNumLabel->setText(QString::number(nodesInfo.length()));
}

void NodeConfigureView::on_applyButton_clicked()
{
    QVector<NodeInfo> nodes;
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        NodeInfo node;
        node.nodeName = ui->tableWidget->item(i, 0)->text();
        node.affiliatedArea = comboboxs.at(i)->currentText();
        node.ID = ui->tableWidget->item(i, 2)->text();
        node.point = ui->tableWidget->item(i, 3)->text();
        nodes.append(node);
    }
    DBOperate db;
    db.updateNodes(nodes);
}

void NodeConfigureView::on_addButton_clicked()
{
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(row + 1);
    ComboBox *combobox = new ComboBox;
    combobox->addItems(this->areas);
    QTableWidgetItem *item = new QTableWidgetItem;
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
    ui->tableWidget->setItem(row, 1, item);
    comboboxs.append(combobox);
    ui->tableWidget->setCellWidget(row, 1, combobox);
}

void NodeConfigureView::on_removeButton_clicked()
{
    int currentRow = ui->tableWidget->currentRow();
    if (currentRow < 0)
    {
        return;
    }
    ui->tableWidget->removeCellWidget(currentRow, 1);
    delete comboboxs.at(currentRow);
    comboboxs.remove(currentRow);
    ui->tableWidget->removeRow(currentRow);
}
